package gqz.testdemo;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.AbsoluteLayout;
import android.widget.ImageView;

/**
 * Created by Administrator on 2018/8/9.
 */

public class TestViewActivity extends Activity {

    String TAG = "TestViewActivity-->";
    AbsoluteLayout lay_abs;
    View cl;
    ImageView dot, rod, record;

    ObjectAnimator rotateOa, rodOa;
    Animation rodAnim;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_testview);
        lay_abs = findViewById(R.id.lay_abs);
        lay_abs.setBackgroundColor(Color.LTGRAY);
        dot = findViewById(R.id.player_dot);
        rod = findViewById(R.id.player_rod);
        record = findViewById(R.id.player_record);
        cl = findViewById(R.id.player_cl);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        setPos();
//        startRotateAnim(record);
//        new Handler().postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                startRodAnim(rod, rod.getX(), rod.getY(), 0, 45);
//            }
//        }, 1000);
    }

    public void setPos() {
        int dotH = dot.getHeight();
        int rodH = rod.getHeight();
        int recordW = record.getWidth();
        int recordH = record.getHeight();
        int layW = recordW + rodH;
        int layH = dotH + rodH + recordH;

        ViewGroup.LayoutParams param = lay_abs.getLayoutParams();
        param.width = layW;
        param.height = layH;
        lay_abs.setLayoutParams(param);
        ViewGroup.LayoutParams cl_param = cl.getLayoutParams();
        cl_param.height = param.height;
        cl.setLayoutParams(cl_param);

        int layHW = layW / 2;
        cl.setX(layHW);
        dot.setX(layHW - dotH / 2);
        dot.setY(0 - dotH / 2);
        rod.setX(layHW);
        record.setX(layHW - recordW / 2);
        record.setY(dotH / 2 + rodH);
    }

    public void startRotateAnim(View view) {
        if (rotateOa == null) {
            rotateOa = ObjectAnimator.ofFloat(view, "rotation", 0, 360);
            rotateOa.setInterpolator(new LinearInterpolator());
            rotateOa.setDuration(5000);
            rotateOa.setRepeatCount(ValueAnimator.INFINITE);
            rotateOa.start();
        } else rotateOa.resume();
    }

    public void stopRotateAnim() {
        if (rotateOa != null && rotateOa.isStarted())
            rotateOa.pause();
    }

    public void startRodAnim(View view, float cx, float cy, float fromDegree, float toDegree) {
        Animation anim = new RotateAnimation(fromDegree, toDegree, Animation.ABSOLUTE, cx, Animation.ABSOLUTE, cy);
        anim.setFillAfter(true); // 设置保持动画最后的状态
        anim.setDuration(700); // 设置动画时间
        anim.setInterpolator(new DecelerateInterpolator()); // 设置插入器，开始慢
        view.startAnimation(anim);
    }

    public void startRodAnim(View view, float fromDegree, float toDegree) {
        view.setPivotX(0);
        view.setPivotY(0);
        rodOa = ObjectAnimator.ofFloat(view, "rotation", fromDegree, toDegree);
        rodOa.setInterpolator(new DecelerateInterpolator());
        rodOa.setDuration(500);
        rodOa.start();
    }

    public void stopRodAnim() {
        if (rodAnim != null && rodAnim.hasStarted())
            rodAnim.cancel();
    }

    public void startPlayAnim() {
        startRotateAnim(record);
//        startRodAnim(rod, rod.getX(), rod.getY(), 0, 45);
        startRodAnim(rod, 0, 45);
    }

    public void stopPlayAnim() {
        stopRotateAnim();
//        stopRodAnim();
//        startRodAnim(rod, rod.getX(), rod.getY(), 45, 0);
        startRodAnim(rod, 45, 0);
    }

    public void onStartClick(View view) {
        startPlayAnim();
    }

    public void onStopClick(View view) {
        stopPlayAnim();
    }
}
